home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: hpwisf1.han.federal.unisys.com!usenet
- From: Bill Kaelin <kaelin@federal.unisys.com>
- Subject: Please Explain this manipulator behavior
- Content-Type: text/plain; charset=us-ascii
- Message-ID: <30EEBF72.4DDD565A@federal.unisys.com>
- Sender: usenet@Federal.Unisys.COM
- Nntp-Posting-Host: h122-70.mcln.federal.unisys.com
- Content-Transfer-Encoding: 7bit
- Cc: mcintire@federal.unisys.com
- Organization: Unisys Federal Systems
- Mime-Version: 1.0
- Date: Sat, 6 Jan 1996 18:29:06 GMT
- X-Mailer: Mozilla 2.0b4 (X11; I; Linux 1.2.13 i486)
-
- Hi,
-
- I've recently been investigating manipulators and have come across
- behavior that I don't understand. The only way to convey the problem is
- by example. I've included the Makefile, main.cc, My_Class_c.hh and
- My_Class_c.cc. Please take a moment to look at these.
-
- ********
- Makefile
- ********
- CFLAGS =-g
- LDFLAGS =
- RM =rm
- TARGET_EXECUTABLE=main
-
- SRCS = \
- My_Class_c.cc \
- main.cc
-
- OBJS =$(SRCS:.cc=.o)
-
-
- $(TARGET_EXECUTABLE): $(OBJS)
- $(CC) $(LDFLAGS) -o $(TARGET_EXECUTABLE) $(OBJS)
-
- clean:
- $(RM) -f core $(OBJS) $(TARGET_EXECUTABLE)
-
- .SUFFIXES:
- .SUFFIXES: .o .cc .hh
- .cc.o:
- $(CC) -c $(CFLAGS) $<
-
-
- ********
- main.cc
- ********
- #include "My_Class_c.hh"
- #include <iomanip.h>
-
- int main(void)
- {
- My_Class_c My_Class;
-
- unsigned int my_uint = 0x00410040;
-
- cout.fill('0');
- cout<<"my_uint is set to: 0x"<<hex<<setw(12)<<my_uint<<"."<<endl;
-
- return(0);
- }
-
- ********
- My_Class_c.hh
- ********
- #if !defined(_My_Class_c_HH)
- #define _My_Class_c_HH
-
- #include <iostream.h>
-
- class My_Class_c : public ostream {
- //class My_Class_c {
- public:
- My_Class_c();
- ~My_Class_c();
- };
-
- #endif // _My_Class_c_HH
-
-
- ********
- My_Class_c.cc
- ********
- #include "My_Class_c.hh"
- #include <iomanip.h>
-
- //
- // Body for Function: My_Class_c
- //
- My_Class_c::My_Class_c()
- {
- unsigned int my_uint1 = 0x0cc00040;
-
- cout.fill('0');
- cout<<"my_uint1 is set to: 0x"<<hex<<setw(12)<<my_uint1<<"."<<endl;
- }
-
-
- //
- // Body for Function: ~Info_c
- //
- My_Class_c::~My_Class_c()
- {
- }
-
- ************************************************************************
-
- As coded, the unexpected output is:
-
- $ ./main
- my_uint1 is set to: 0x64000213909568.
- my_uint is set to: 0x000000410040.
- $
-
- If change the class definintion in My_Class_c.hh, so that My_Class_c
- does not inherit from ostream, e.g. change the two lines in
- My_Class_c.hh to:
-
- //class My_Class_c : public ostream {
- class My_Class_c {
-
- Then the output is:
-
- $ ./main
- my_uint1 is set to: 0x00000cc00040.
- my_uint is set to: 0x000000410040.
- $
-
-
- Therefore, somehow by inheriting from ostream, the "hex" manipulator no
- longer modifies the output to be in hex. Instead a mysterious "64" is
- output and the following value is output in decimal instead of hex.
-
- If I don't inherit from ostream, then the output is as expected. Note
- that the setw manipulator works as expected in both cases.
-
- I've compiled and executed this example with 4 different C++ compilers
- and all exhibit this unusual behavior (Sun CC, Centerline CC, dynix/ptx
- CC, and gnu g++ under Linux).
-
- I've done some sleuthing into this problem. Namely, looking in the
- iostream.h file, there is an enum which has "hex" as one of its members.
- Interestingly, this hex enum has the value of 0x0040. In decimal this
- is 64! Therefore, it is my belief that by inheriting from ostream,
- "hex" takes on the value of 64(decimal) which is output instead of being
- the manipulator to modify the stream to output in hex.
-
- Is this the appropriate behavior for C++?? If this is true, how can one
- inherit from ostream and still output hexadecimal values?
-
- Please reply with e-mail.
-
- Thanks in advance.
-
- --------------------------------------------------------------
- Bill Kaelin (kaelin@federal.unisys.com) Unisys Federal Systems
- (703) 620-7062 (USA) 12010 Sunrise Valley Drive
- (703) 620-7833 (USA FAX) Reston, VA 22091-3498
- Opinions are my own not UFS's.
-